//
//  StringExtension.swift
//  DateNote
//
//  Created by 杨立鹏 on 2018/3/4.
//  Copyright © 2018年 杨立鹏. All rights reserved.
//

import Foundation


extension String {
    

    
    /// 国际化
    var i18n : String {
        return NSLocalizedString(self, comment: "")
    }
    
    
    /**
     生成随机子字符串,
     
     - parameter length: 生成的字符串的长度
     
     - returns: 随机生成的字符串
     */
    func randomSubString(length: Int) -> String {
        
        var ranStr = ""
        for _ in 0..<length {
            let index = Int(arc4random_uniform(UInt32(self.count)))
            
            for c in self {
                if self.index(of: c)?.encodedOffset == index {
                    ranStr += String(c)
                }
                
            }
        }
        return ranStr
    }
    /// range转换为NSRange
    func nsRange(from range: Range<String.Index>) -> NSRange {
        return NSRange(range, in: self)
    }

}    
